/**
 * Created by xlt on 2017/5/8.
 */
import React from "react";
import ReactDOM from 'react-dom';
import { Link } from 'react-router';
import { connect } from 'react-redux';
import { fetchPosts,reset } from '../../actions';

class appPay extends React.Component {
    constructor(props) {
        super(props)
    }

    componentWillMount() {
        document.title = "叮叮收钱";
    }

    componentDidMount() {
    }

    componentWillReceiveProps(nextProps) {
        if(nextProps.payResult.errorCode === 0) {
            window.location.href = nextProps.payResult.data;
        }
    }

    payClick(event){
        //let payAmount = ReactDOM.findDOMNode(this.refs.payAmount).value;
        const header= { "X-Client-Agent":"weixin", "X-APIVersion":"2.0", "X-Client-ID":'123456'}
        const params = {"thridpartyUrl" : "http://weixin.hzyisu.com/#/ding/paySuccess", "mchntId":"YS20170600001","orderAmt":"0.01","tradeType":"WECHAT","openId":"oeOEswzsn33bPXIimZmevRRxTx5E"};
        this.props.dispatch(fetchPosts("onlinePay", header, params));
        event.preventDefault();
        event.stopPropagation();
    }

    render() {
        return (
            <div>
                <ul className="address-list">
                    <li>支付金额: <input type="text" name="payAmount" ref="payAmount" /></li>
                </ul>
                <div className="address-add">
                    <Link href="javascript:;" onClick={this.payClick.bind(this)}>提交</Link>
                </div>
            </div>
        )
    }
}

function mapStateToProps(state){
    const { postsByApi } = state
    const {
        isFetching,
        items: payResult
    } = postsByApi["onlinePay"] || {
        isFetching: false,
        items: {}
    }
    return {
        payResult,
        isFetching
    }
}

export default connect(mapStateToProps)(appPay);